// 记录以点击红包的oid
var arr = [];
// 记录监测红包的次数
var count = 0;
// 记录普通红包的点击次数
var clickcount = 0;
// 记录猜拳红包的点击次数
var clickcount1 = 0;
// 记录红包金额
var num = 0;
function wait(delay) {
  return new Promise(resolve => setTimeout(resolve, delay));
}
const getData = (oId, gesture, types) => {
  console.log(oId, gesture);
  if(oId === null || oId == '' || oId === undefined){
    console.log("oid无效："+ oId)
    return;
  }
  $.ajax({
    url: '/chat-room/red-packet/open',
    type: 'post',
    data: JSON.stringify({
      oId,
      gesture,
      dice: {}
    }),
    dataType: "json",
    success: (res) => {
      console.log(res);
      if(types == "猜拳"){
        if(res.who[0].userName == 'ly-yy'){
          num += res.who[0].userMoney;
          console.log("抢了："+ res.who[0].userMoney);
        }
      }else if(types == "普通"){
        res.who.forEach(val=>{
          if(val.userName == 'ly-yy'){
            num += val.userMoney;
            console.log("抢了："+ val.userMoney);
          }
        })
      }
      console.log("一共抢了："+ num + "积分");
    },
    error: (exp) => {
      console.log(exp);
    },
  })
}
function shuffle(array) {
  for (let i = array.length - 1; i > 0; i--) {
    let j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array;
}

function getRandomNumber(arr) {
  var randomIndex = Math.floor(Math.random() * arr.length);
  return arr[randomIndex];
}



setInterval(async function () {
  // count++;
  // console.log("检测了:"+count+"次")
  // 判断红包是否存在，判断红包的类型，记录红包的oid防止重复点击
  if (document.querySelector('div.hongbao__item') != null) {
    document.querySelectorAll('div.hongbao__item').forEach(async val => {
      // console.log(val.className.indexOf("opened") == -1)
      // console.log(val.style.opacity != '0.36')
      // console.log(val.className.indexOf("opened") == -1 ||  val.style.opacity != '0.36')
      // if (val.className.indexOf("opened") == -1 ||  val.style.opacity != '0.36') {
      if (val.style.opacity != '0.36') {
        if (val.getAttribute('onclick') !== null) {
          if (val.getAttribute('onclick').indexOf("('") != -1) {
            const oid = val.getAttribute('onclick').split("'")[1];
            if (arr.filter(val1 => val1 == oid).length === 0) {
              arr.push(oid);
              console.log(val.getAttribute('onclick'))
              await wait(0);
              getData(oid, 0 ,'普通')
              clickcount++;
              console.log("普通红包点击了:" + clickcount + "次")
            }
          } else {
            const oid = val.getAttribute('onclick').split('(')[1].split(')')[0];
            if (arr.filter(val1 => val1 == oid).length === 0) {
              arr.push(oid);
              console.log(val.getAttribute('onclick'))
              await wait(0);
              getData(oid, 0,'普通')
              clickcount++;
              console.log("普通红包点击了:" + clickcount + "次")
            }
          }
        } else {
          if (val.children[val.children.length - 1].children[1].textContent.trim() <= 100) {
            const oid = val.children[0].children[[0, 1, 2][Math.floor(Math.random() * 3)]].getAttribute("onclick").split(",")[0].split("(")[3];
            if (arr.filter(val1 => val1 == oid).length === 0) {
              arr.push(oid);
              console.log(val.children[0].children[[0, 1, 2][Math.floor(Math.random() * 3)]].getAttribute("onclick"))
              await wait(1500);
              getData(oid, shuffle([2, 1, 0])[getRandomNumber(shuffle([1, 0, 2]))],'猜拳')
              clickcount1++;
              console.log("猜拳红包点击了:" + clickcount1 + "次")
            }
          }
        }
      }
    });
  }
});